package toanstt.gameplay;

import toanstt.com.DiamondLinkinActivity;
import toanstt.com.Sound2;
import toanstt.com.T;
import toanstt.com.Vec2;
import toanstt.sprites.Sprite;
import toanstt.sprites.Sprites;
import android.R;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.Log;
import android.view.MotionEvent;

public class GameManagement 
{
	public static CellTable celltable;
	SelectedCell selectedcell;
	GamePannel gamepanel;
	public static TimeLine timeline;
	public static Comment comment;
	int temp_x, temp_y;
	Vec2 vec_temp;
	public static Skill skill;
	public ManaView manaview;
	
	
	
	public GameManagement()
	{
		comment = new Comment();
		selectedcell = new SelectedCell();
		
		//Sound2.addSound(mContext, index, SoundID)
		//a = new Sound2();
		//a.addSound(1,0);
	
		//NewGame(1,1,1,1);
		//NewGame(1,1,1,1);
	}
	public void  NewGame(int stage,int time,int level)
	{
		//T.GAME_NUM_DIAMOND = level;
		
		T.GAME_NUM_DIAMOND = (int)(stage*1.0f/20+4);
		if(T.GAME_NUM_DIAMOND>T.GAME_NUM_DIAMOND_MAX)
			T.GAME_NUM_DIAMOND = T.GAME_NUM_DIAMOND_MAX;
		
		T.PLAY_SCORE =0;
		if(stage==0)	
		{
			timeline = new TimeLine(time);
			T.dialog.DoDialog("New Game");
		}
		else
		{
			timeline = new TimeLine(T.stagesmaxscore.stages[stage-1],true);
			T.dialog.DoDialog("Stage " + stage);
		}
		T.PLAY_UNLUCKY = T.PLAY_CURRENTSTAGE/100+1;
		celltable = new CellTable(); 
		//selectedcell = new SelectedCell();
		selectedcell.NewGame();
		//comment = new Comment();
		gamepanel = new GamePannel();
		skill = new Skill();
		manaview = new ManaView((int)(T.PANEL_MIN_X+T.UNIT), (int)(T.PANEL_MIN_Y + 27*T.UNIT), (int)(12*T.UNIT), (int)(2*T.UNIT));
		T.PLAY_CURRENT_MANA = T.PLAY_MANASIZE;
		
		
		//T.PLAY_LUCKY = 50;
		//T.PLAY_UNLUCKY = 50;
		//T.sound.Play(T.sound.THEME);
		
		
		//Sound2.playSound(0);
	
		//Sound2.playSound(2);
		//Sound2.playSound(3);
		
		//a.playSound(1);
		//T.sound.PlayLoop(T.sound.THEME);
		//T.sound.Play(T.sound.THEME);
		T.sound1.Play();
	}
	public void Draw(Canvas canvas)
	{
		DrawMainFram(canvas);
		celltable.Draw(canvas);
		selectedcell.Draw(canvas);
		comment.Draw(canvas);
		gamepanel.Draw(canvas);
		timeline.Draw(canvas);
		T.dialog.Draw(canvas);
		skill.Draw(canvas);
		manaview.Draw(canvas);
		//test1.drawAnim(canvas);
		
	}
	public void Update()
	{
		skill.Update();
		T.vibra.Update();
		celltable.Update();
		timeline.Tick();
		manaview.Update();
	}
	public void touch(MotionEvent mo)
	{
		//Log.d("here", "ddddddd");
		int action = mo.getAction();
		int actionCode = action & MotionEvent.ACTION_MASK;
		temp_x = (int)mo.getX();
		temp_y = (int)mo.getY();
		
		skill.touch(mo);
		if(T.USE_BOOM == false && T.USE_MAGIC == false)
		{
			if(temp_x>T.PANEL_MIN_X &&temp_x < T.PANEL_MAX_X)
			{
				if(temp_y>T.PANEL_MIN_Y &&temp_y < T.PANEL_MAX_Y)
					gamepanel.touch(mo);
			}
			else
			if(celltable.need_update_position==0)
			if(temp_x>T.GAMESCREEN_MIN_X &&temp_x < T.GAMESCREEN_MAX_X)
			{
				if(temp_y>T.GAMESCREEN_MIN_Y &&temp_y < T.GAMESCREEN_MAX_Y)
				{
					selectedcell.MouseEent(mo);
					
				}
			}
		}
				
		vec_temp =  CellTable.Getcellwithcoor((int)mo.getX(),(int)mo.getY());
		
		switch(actionCode)
		{
		case MotionEvent.ACTION_DOWN:
			
	    	break;
		case MotionEvent.ACTION_UP:
			selectedcell.MouseUp(vec_temp.x, vec_temp.y);
			
			break;
		
		case MotionEvent.ACTION_MOVE:
			
			break;

		}
		
		
		
	}
	protected void DrawMainFram(Canvas canvas)
	{
		int width_step=T.MARGIN_WIDTH;
		//draw border left
		T.paint.setColor(Color.DKGRAY);
		canvas.drawRect(width_step, 0, width_step + T.BORDER_WIDTH, T.DISPLAY_HEIGHT, T.paint);
		//draw game screen
		width_step += T.BORDER_WIDTH;
		canvas.drawARGB(150, 0, 0,0);
		//T.paint.setColor(Color.WHITE);
		//canvas.drawRect(width_step, 0, width_step + T.GAME_SCREEN_WIDTH, T.GAME_SCREEN_HEIGHT, T.paint);
		
		//draw boder right
		width_step += T.GAME_SCREEN_WIDTH;
		T.paint.setColor(Color.DKGRAY);
		canvas.drawRect(width_step, 0, width_step + T.BORDER_WIDTH, T.DISPLAY_HEIGHT, T.paint);
		//draw menu
		//width_step += T.BORDER_WIDTH;
		//T.paint.setColor(Color.LTGRAY);
		//canvas.drawRect(width_step, 0, width_step + T.MENU_WIDTH, T.DISPLAY_HEIGHT, T.paint);
		T.mat = new Matrix();
		T.mat.postScale(T.MENU_WIDTH*1.0f/300,T.MENU_WIDTH*2.0f/600);
		T.mat.postTranslate(T.PANEL_MIN_X, T.PANEL_MIN_Y);
		canvas.drawBitmap(T.bitmap_frame_panel, T.mat, null);
		//T.bitmapFont[0].drawString(canvas, "toan", 800, 200,0);
		///T.bitmapFont[1].drawString(canvas, "toan", 800, 300,0);
		//T.bitmapFont[2].drawString(canvas, "toan", 800, 400,0);
		////T.bitmapFont[3].drawString(canvas, "toan", 800, 500,0);
		//T.bitmapFont[4].drawString(canvas, "toan", 800, 100,0);
	}
}
